import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'pages/notification_page.dart';
import 'pages/profile_page.dart';
import 'pages/medication_schedule_page.dart';

class Home1_3Page extends StatelessWidget {
  const Home1_3Page({Key? key}) : super(key: key);

  Future<void> _logout(BuildContext context) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setBool('isLoggedIn', false);
    Navigator.pushNamedAndRemoveUntil(context, '/home0', (route) => false);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: const Color(0xFF1DC4A0),
        title: const Text(
          '用户端',
          style: TextStyle(color: Colors.white),
        ),
        leading: IconButton(
          icon: const Icon(
            Icons.arrow_back,
            color: Colors.white, // 设置箭头颜色为白色
          ),
          onPressed: () => Navigator.pop(context), // 设置返回功能
        ),
        actions: [
          IconButton(
            icon: Image.asset('assets/icons/logout.png', width: 96, height: 96),
            onPressed: () => _logout(context),
          ),
        ],
      ),
      body: Container(
        width: double.infinity,
        height: double.infinity,
        color: Colors.white,
        child: SafeArea(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // 顶部区域
              Container(
                width: double.infinity,
                padding: const EdgeInsets.all(40),
                decoration: const BoxDecoration(
                  gradient: LinearGradient(
                    begin: Alignment.topCenter,
                    end: Alignment.bottomCenter,
                    colors: [Color(0xFF1DC4A0), Color(0xFF1DC4A0)],
                  ),
                  borderRadius: BorderRadius.only(
                    bottomLeft: Radius.circular(40),
                    bottomRight: Radius.circular(40),
                  ),
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    // 顶部栏
                    Row(
                      children: [
                        // 用户头像
                        Container(
                          width: 45,
                          height: 45,
                          decoration: BoxDecoration(
                            shape: BoxShape.circle,
                            border: Border.all(color: Colors.white, width: 2),
                          ),
                          child: ClipOval(
                            child: Image.asset(
                              'assets/icons/patient_home0.png',
                              fit: BoxFit.cover,
                            ),
                          ),
                        ),
                        const SizedBox(width: 12),
                        // 欢迎文本
                        const Text(
                          '您好，用户a',
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 20,
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                        const Spacer(),
                      ],
                    ),
                    const SizedBox(height: 10),
                  ],
                ),
              ),

              // 医疗服务标题
              const Padding(
                padding: EdgeInsets.fromLTRB(20, 20, 20, 2),
                child: Text(
                  '医疗服务',
                  style: TextStyle(
                    fontSize: 20,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),

              // 选项卡片区域
              Expanded(
                child: Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 27),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      _buildServiceCard(
                        title: '病因诊断',
                        iconPath: 'assets/icons/icon1_home1-3.png',
                        color: const Color(0xFF8F9EFF),
                        onTap: () {
                          Navigator.pushNamed(context, '/diagnosis');
                        },
                      ),
                      const SizedBox(height: 40),
                      _buildServiceCard(
                        title: '风险评估',
                        iconPath: 'assets/icons/icon2_home1-3.png',
                        color: const Color(0xFFFEAA48),
                        onTap: () {
                          Navigator.pushNamed(context, '/risk_assesment');
                        },
                      ),
                      const SizedBox(height: 40),
                      _buildServiceCard(
                        title: '健康跟踪',
                        iconPath: 'assets/icons/icon3_home1-3.png',
                        color: const Color(0xFFFF8FA3),
                        onTap: () {
                          Navigator.pushNamed(context, '/health_tracking');
                        },
                      ),
                    ],
                  ),
                ),
              ),

              // 底部导航栏
              Container(
                padding: const EdgeInsets.symmetric(vertical: 12),
                decoration: BoxDecoration(
                  color: Colors.white,
                  boxShadow: [
                    BoxShadow(
                      color: Colors.black.withOpacity(0.1),
                      blurRadius: 10,
                      offset: const Offset(0, -2),
                    ),
                  ],
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    _buildNavItem(
                      iconPath: 'assets/icons/home.png',
                      selectedIconPath: 'assets/icons/home.png',
                      label: '首页',
                      isSelected: true,
                      onTap: () {},
                    ),
                    _buildNavItem(
                      iconPath: 'assets/icons/calendar.png',
                      selectedIconPath: 'assets/icons/calendar.png',
                      label: '服药日程',
                      isSelected: false,
                      onTap: () => Navigator.pushReplacementNamed(
                          context, '/medication_schedule'),
                    ),
                    _buildNavItem(
                      iconPath: 'assets/icons/notification.png',
                      selectedIconPath: 'assets/icons/notification.png',
                      label: '消息',
                      isSelected: false,
                      onTap: () {
                        Navigator.pushReplacementNamed(
                            context, '/notification');
                      },
                    ),
                    _buildNavItem(
                      iconPath: 'assets/icons/profile.png',
                      selectedIconPath: 'assets/icons/profile.png',
                      label: '我的',
                      isSelected: false,
                      onTap: () {
                        Navigator.pushReplacementNamed(context, '/profile');
                      },
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildServiceCard({
    required String title,
    required String iconPath,
    required Color color,
    required VoidCallback onTap,
  }) {
    return Material(
      color: Colors.transparent,
      child: InkWell(
        onTap: onTap,
        borderRadius: BorderRadius.circular(15),
        child: Container(
          width: double.infinity,
          height: 110,
          padding: const EdgeInsets.symmetric(horizontal: 25),
          decoration: BoxDecoration(
            color: color,
            borderRadius: BorderRadius.circular(15),
            boxShadow: [
              BoxShadow(
                color: color.withOpacity(0.3),
                blurRadius: 8,
                offset: const Offset(0, 4),
              ),
            ],
          ),
          child: Row(
            children: [
              // 图标
              Container(
                width: 60,
                height: 60,
                padding: const EdgeInsets.all(12),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(12),
                ),
                child: Image.asset(
                  iconPath,
                  width: 36,
                  height: 36,
                ),
              ),
              const SizedBox(width: 20),
              // 文本
              Text(
                title,
                style: const TextStyle(
                  color: Colors.white,
                  fontSize: 20,
                  fontWeight: FontWeight.w600,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildNavItem({
    required String iconPath,
    required String selectedIconPath,
    required String label,
    required bool isSelected,
    required VoidCallback onTap,
  }) {
    return GestureDetector(
      onTap: onTap,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Stack(
            alignment: Alignment.bottomCenter,
            children: [
              // 图标
              Image.asset(
                iconPath,
                width: 24,
                height: 24,
                color: isSelected ? const Color(0xFF1DC4A0) : Colors.grey,
              ),
              // 选中指示器
              if (isSelected)
                Positioned(
                  bottom: -8,
                  child: Image.asset(
                    'assets/icons/chosen.png',
                    width: 16,
                    height: 3,
                  ),
                ),
            ],
          ),
          const SizedBox(height: 8),
          Text(
            label,
            style: TextStyle(
              color: isSelected ? const Color(0xFF1DC4A0) : Colors.grey,
              fontSize: 12,
              fontWeight: isSelected ? FontWeight.w600 : FontWeight.normal,
            ),
          ),
        ],
      ),
    );
  }
}
